<template>
  <!-- 这是网站管理下的轮播图管理 -->
  <div class="image_place">
    <el-row :gutter="15">
      <el-col :span="24">
        <div class="grid-content bg-purple">
          当前展示的轮播图
        </div>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="6">
        <div class="block">
          <span class="title">图片1</span>
          <el-image :src="pic1" />
        </div>
        <div class="uppic1">
          <span class="tips">修改图片1</span>
          <el-upload
            class="avatar-uploader"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="imageUrl" :src="imageUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon" />
          </el-upload>
        </div>
      </el-col>

      <el-col :span="6">
        <div class="block">
          <span class="title">图片2</span>
          <el-image :src="pic2" />
        </div>
        <div class="uppic1">
          <span class="tips">修改图片2</span>
          <el-upload
            class="avatar-uploader"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="imageUrl" :src="imageUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon" />
          </el-upload>
        </div>
      </el-col>

      <el-col :span="6">
        <div class="block">
          <span class="title">图片3</span>
          <el-image :src="pic3" />
        </div>
        <div class="uppic1">
          <span class="tips">修改图片3</span>
          <el-upload
            class="avatar-uploader"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="imageUrl" :src="imageUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon" />
          </el-upload>
        </div>
      </el-col>

      <el-col :span="6">
        <div class="block">
          <span class="title">图片4</span>
          <el-image :src="pic4" />
        </div>
        <div class="uppic1">
          <span class="tips">修改图片4</span>
          <el-upload
            class="avatar-uploader"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="imageUrl" :src="imageUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon" />
          </el-upload>
        </div>
      </el-col>
    </el-row>
  </div>

</template>

<script>
import { getPicture } from '@/api/picture'

export default {
  data() {
    return {
      Picture: [],
      pic1: '',
      pic2: '',
      pic3: '',
      pic4: '',
      imageUrl: ''
    }
  },
  created() {
    this.loadPicture()
  },
  mounted() {},
  methods: {
    // 获取当前轮播图
    loadPicture() {
      getPicture().then(res => {
        console.log(res)
        this.Picture = res.data
        console.log(this.Picture[1].picLink)
        this.pic1 = this.Picture[0].picLink
        this.pic2 = this.Picture[1].picLink
        this.pic3 = this.Picture[2].picLink
        this.pic4 = this.Picture[3].picLink
      })
    },
    // 上传轮播图(未完善)
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw)
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      // const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传图片只能是 JPG 格式!')
      }
      // if (!isLt2M) {
      //   this.$message.error('上传头像图片大小不能超过 2MB!');
      // }
      // return isJPG && isLt2M;
      return isJPG
    }
  }
}
</script>

<style scoped>
  .image_place {
    background-color: #e5e9f2;
    border-radius: 4px
  }
  .el-image {
    width: 275px;
    height: 150px
  }
  .el-row {
    margin-bottom: 20px;
  }
  .el-col {
    border-radius: 4px;
  }
  .bg-purple-dark {
    background: #99a9bf;
  }
  .bg-purple {
    background: #d3dce6;
  }
  .bg-purple-light {
    background: #e5e9f2;
  }
  .grid-content {
    font-size: 20px;
    border-radius: 4px;
    min-height: 20px;
    text-align: center;
    font-weight: bold;
  }
  .row-bg {
    padding: 10px 0;
    background-color: #f9fafc;
  }
  .title {
    font-size: 20px;
    color: light_gray;
    margin: 0px auto 40px auto;
    text-align: center;
    font-weight: bold;
    padding-left: 40%;
  }
  .block {
    padding: 10px;
  }
  .tips {
    font-size: 20px;
    color: light_gray;
    margin: 0px auto 40px auto;
    text-align: center;
    font-weight: bold;
    padding-left: 30%;
    padding-top: 50%;
  }
</style>

<style scoped>
.uppic {
  border: 1px;

}
.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 300px;
    height: 300px;
    display: block;
  }
</style>
